package com.yby.mall.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.yby.common.dto.SkuFullReductionDto;
import com.yby.common.dto.SkuLadderDto;
import com.yby.common.dto.SpuBoundsDto;
import com.yby.common.utils.PageUtils;
import com.yby.common.utils.R;
import com.yby.mall.product.dao.*;
import com.yby.mall.product.entity.*;
import com.yby.mall.product.feign.CouponFeignService;
import com.yby.mall.product.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yby.mall.product.service.SpuInfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {
    @Autowired
    private SpuInfoDao spuInfoDao;
    @Autowired
    private SpuInfoDescDao spuInfoDescDao;
    @Autowired
    private SpuImagesDao spuImagesDao;
    @Autowired
    private SkuImagesDao skuImagesDao;
    @Autowired
    private AttrDao attrDao;
    @Autowired
    private ProductAttrValueDao productAttrValueDao;
    @Autowired
    private CouponFeignService couponFeignService;
    @Autowired
    private SkuInfoDao skuInfoDao;
    @Autowired
    private SkuSaleAttrValueDao skuSaleAttrValueDao;


    @Override
    public R queryPage(Integer page, Integer limit, Long brandId, Long catelogId, Integer status, String key) {
        PageHelper.startPage(page, limit);
        List<SpuInfoEntity> list = spuInfoDao.selectList(Wrappers.<SpuInfoEntity>lambdaQuery()
                .eq(!ObjectUtils.isEmpty(brandId) && brandId != 0, SpuInfoEntity::getBrandId, brandId)
                .eq(!ObjectUtils.isEmpty(catelogId) && catelogId != 0, SpuInfoEntity::getCatalogId, catelogId)
                .eq(!ObjectUtils.isEmpty(status), SpuInfoEntity::getPublishStatus, status)
                .like(!StringUtils.isEmpty(key), SpuInfoEntity::getSpuName, key));
        return R.ok().put("page", PageUtils.restPage(list));
    }

    @Override
    @Transactional
    public R saveSpuInfo(SpuInfoVo spuInfoVo) {
        //向pms_spu_info表中保存信息
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(spuInfoVo,spuInfoEntity,"decript","images","bounds","baseAttrs","skus");
        spuInfoDao.insert(spuInfoEntity);

        //向pms_spu_info_desc中保存信息
        SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
        List<String> decript = spuInfoVo.getDecript();
        String dedecriptText = JSONObject.toJSONString(decript);
        spuInfoDescEntity.setDecript(dedecriptText);
        spuInfoDescEntity.setSpuId(spuInfoEntity.getId());
        spuInfoDescDao.insert(spuInfoDescEntity);

        //向pms_spu_images中保存信息
        List<String> spuImgList = spuInfoVo.getImages();
        spuImgList.stream().map(imgUrl ->{
            SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
            String imgName = imgUrl.substring(imgUrl.lastIndexOf("/"), imgUrl.length() - 1);
            spuImagesEntity.setImgName(imgName);
            spuImagesEntity.setImgUrl(imgUrl);
            spuImagesEntity.setSpuId(spuInfoEntity.getId());
            return spuImagesEntity;
        }).forEach(spuImagesEntity -> {
            spuImagesDao.insert(spuImagesEntity);
        });

        //向pms_product_attr_value表中保存信息
        List<BaseAttrs> baseAttrsList = spuInfoVo.getBaseAttrs();
        baseAttrsList.stream().forEach(baseAttrs -> {
            ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
            productAttrValueEntity.setAttrId(baseAttrs.getAttrId());
            productAttrValueEntity.setAttrValue(baseAttrs.getAttrValues());
            productAttrValueEntity.setAttrSort(baseAttrs.getShowDesc());
            productAttrValueEntity.setSpuId(spuInfoEntity.getId());
            AttrEntity attrEntity = attrDao.selectById(baseAttrs.getAttrId());
            productAttrValueEntity.setAttrName(attrEntity.getAttrName());
            productAttrValueDao.insert(productAttrValueEntity);
        });

        List<Skus> skusList = spuInfoVo.getSkus();
        skusList.stream().forEach(skus -> {
            //向pms_sku_info表中保存信息
            SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
            skuInfoEntity.setSpuId(spuInfoEntity.getId());
            skuInfoEntity.setSkuName(skus.getSkuName());
            skuInfoEntity.setSkuTitle(skus.getSkuTitle());
            skuInfoEntity.setSkuSubtitle(skus.getSkuSubtitle());
            skuInfoEntity.setPrice(skus.getPrice());
            skuInfoEntity.setBrandId(spuInfoVo.getBrandId());
            skuInfoEntity.setCatalogId(spuInfoVo.getCatalogId());
            skuInfoDao.insert(skuInfoEntity);

            //向pms_sku_sale_attr_value表中保存信息
            List<Attr> attrList = skus.getAttr();
            attrList.stream().forEach(attr -> {
                SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
                skuSaleAttrValueEntity.setAttrId(attr.getAttrId());
                skuSaleAttrValueEntity.setAttrName(attr.getAttrName());
                skuSaleAttrValueEntity.setAttrValue(attr.getAttrValue());
                skuSaleAttrValueEntity.setSkuId(skuInfoEntity.getSkuId());
                skuSaleAttrValueDao.insert(skuSaleAttrValueEntity);
            });

            //向pms_sku_images表中保存信息
            List<Images> imagesList = skus.getImages();
            imagesList.stream().forEach(images -> {
                SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                skuImagesEntity.setSkuId(skuInfoEntity.getSkuId());
                skuImagesEntity.setImgUrl(images.getImgUrl());
                skuImagesEntity.setDefaultImg(images.getDefaultImg());
                skuImagesDao.insert(skuImagesEntity);
                if(images.getDefaultImg() == 1){
                    skuInfoDao.updateByDefaultImg(skuInfoEntity.getSkuId(),images.getImgUrl());
                }
            });

            //向sms_sku_ladder表中保存信息
            SkuLadderDto skuLadderDto = new SkuLadderDto();
            skuLadderDto.setSkuId(skuInfoEntity.getSkuId());
            skuLadderDto.setDiscount(skus.getDiscount());
            skuLadderDto.setFullCount(skus.getFullCount());
            couponFeignService.saveSkuLadder(skuLadderDto);

            //向sms_sku_full_reduction中保存数据
            SkuFullReductionDto skuFullReductionDto = new SkuFullReductionDto();
            skuFullReductionDto.setSkuId(skuInfoEntity.getSkuId());
            skuFullReductionDto.setFullPrice(skus.getFullPrice());
            skuFullReductionDto.setReducePrice(skus.getReducePrice());
            couponFeignService.saveSkuFullReduction(skuFullReductionDto);
        });

        //向sms_spu_bounds表中保存信息
        SpuBoundsDto spuBoundsDto = new SpuBoundsDto();
        BeanUtils.copyProperties(spuInfoVo.getBounds(),spuBoundsDto);
        spuBoundsDto.setSpuId(spuInfoEntity.getId());
        couponFeignService.saveSpubounds(spuBoundsDto);
        return R.ok();
    }

    @Override
    public R spuUp(Long spuId) {
        spuInfoDao.updateByStatus(spuId,1);
        return R.ok();
    }
}